//
//  CustomAnnotationView.m
//  flutter_baidu_mapapi_map
//
//  Created by lly on 2021/10/2.
//

#import "CustomAnnotationView.h"

#define kPortraitMargin     0
#define kPortraitWidth      0
#define kPortraitHeight     0

#define kTitleWidth         70
#define kTitleHeight        30
#define kArrorHeight        10

@interface CustomCalloutView ()

//@property (nonatomic, strong) UIImageView *portraitView;
//@property (nonatomic, strong) UILabel *subtitleLabel;
@property (nonatomic, strong) UIButton *titleLabel;

@end

@implementation CustomCalloutView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews
{
//    // 添加图片，即商户图
//    self.portraitView = [[UIImageView alloc] initWithFrame:CGRectMake(kPortraitMargin, kPortraitMargin, kPortraitWidth, kPortraitHeight)];
//
//    self.portraitView.backgroundColor = [UIColor blackColor];
//    [self addSubview:self.portraitView];
    
    // 添加标题，即商户名
    self.titleLabel = [[UIButton alloc] initWithFrame:CGRectMake(kPortraitMargin * 2 + kPortraitWidth, kPortraitMargin, kTitleWidth, kTitleHeight)];
    self.titleLabel.titleLabel.font = [UIFont boldSystemFontOfSize:13];
//    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.titleLabel.textColor = [UIColor blackColor];
//    [self.titleLabel addTarget:self action:@selector(tap) forControlEvents:UIControlEventTouchUpInside];
//    self.titleLabel.text = @"";
    [self addSubview:self.titleLabel];
    
//    // 添加副标题，即商户地址
//    self.subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin * 2 + kPortraitWidth, kPortraitMargin * 2 + kTitleHeight, kTitleWidth, kTitleHeight)];
//    self.subtitleLabel.font = [UIFont systemFontOfSize:12];
//    self.subtitleLabel.textColor = [UIColor lightGrayColor];
//    self.subtitleLabel.text = @"subtitleLabelsubtitleLabelsubtitleLabel";
//    [self addSubview:self.subtitleLabel];
}

- (void)addTap:(id)target sel:(SEL)sel{
    [self.titleLabel addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
}

- (void)setTitle:(NSString *)title
{
    [self.titleLabel setTitle:title forState:UIControlStateNormal];
}

- (void)drawRect:(CGRect)rect
{

    [self drawInContext:UIGraphicsGetCurrentContext()];

    self.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.layer.shadowOpacity = 1.0;
    self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);

}

- (void)drawInContext:(CGContextRef)context
{

    CGContextSetLineWidth(context, 2.0);
    CGContextSetFillColorWithColor(context, [UIColor colorWithRed:1 green:1 blue:1 alpha:0.8].CGColor);

    [self getDrawPath:context];
    CGContextFillPath(context);

}

- (void)getDrawPath:(CGContextRef)context
{
    CGRect rrect = self.bounds;
    CGFloat radius = 6.0;
    CGFloat minx = CGRectGetMinX(rrect),
    midx = CGRectGetMidX(rrect),
    maxx = CGRectGetMaxX(rrect);
    CGFloat miny = CGRectGetMinY(rrect),
    maxy = CGRectGetMaxY(rrect)-kArrorHeight;

    CGContextMoveToPoint(context, midx+kArrorHeight, maxy);
    CGContextAddLineToPoint(context,midx, maxy+kArrorHeight);
    CGContextAddLineToPoint(context,midx-kArrorHeight, maxy);

    CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);
    CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, maxx, radius);
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
    CGContextClosePath(context);
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
    return YES;
}

//- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
//    UIView *view = [super hitTest:point withEvent:event];
//    if (view == nil) {
//        CGPoint tempoint = [self.titleLabel convertPoint:point fromView:self];
//        if (CGRectContainsPoint(self.titleLabel.bounds, tempoint))
//        {
//            view = self.titleLabel;
//        }
//    }
//    return self;
//}


- (void)setMarkerId:(NSString *)markerId{
    _titleLabel.tag = [markerId integerValue];
}

@end

@interface CustomAnnotationView ()

@property (nonatomic, strong, readwrite) CustomCalloutView *calloutView;

@end

#define kCalloutWidth       70.0
#define kCalloutHeight      40.0

@implementation CustomAnnotationView

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
    CGPoint tempoint = [self.calloutView convertPoint:point fromView:self];
    if (CGRectContainsPoint(self.calloutView.bounds, tempoint)){
        return YES;
    }else{
        return [super pointInside:point withEvent:event];
    }

}

- (CustomCalloutView *)calloutView {
    if(_calloutView == nil){
        self.calloutView = [[CustomCalloutView alloc] initWithFrame:CGRectMake(0, 0, kCalloutWidth, kCalloutHeight)];
        self.calloutView.userInteractionEnabled = YES;
        [self addSubview:self.calloutView];
    }
    return  _calloutView;
}

//- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
//    UIView *view = [super hitTest:point withEvent:event];
//    if (view == nil) {
//        CGPoint tempoint = [self.calloutView convertPoint:point fromView:self];
//        if (CGRectContainsPoint(self.calloutView.bounds, tempoint))
//        {
//            view = self.calloutView;
//        }
//    }
//    return view;
//}

- (void)setAnnotation:(id<BMKAnnotation>)annotation {
//    [super setAnnotation:annotation];
    self.calloutView.title = annotation.title;
}
@end
